package cn.cjj.gulimall.product.service.impl;

import cn.cjj.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.cjj.gulimall.product.entity.AttrEntity;
import cn.cjj.gulimall.product.entity.ProductAttrValueEntity;
import cn.cjj.gulimall.product.service.AttrAttrgroupRelationService;
import cn.cjj.gulimall.product.service.AttrService;
import cn.cjj.gulimall.product.service.ProductAttrValueService;
import cn.cjj.gulimall.product.vo.AttrGroupWithAttrsVo;
import cn.cjj.gulimall.product.vo.SkuItemVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cjj.common.utils.PageUtils;
import cn.cjj.common.utils.Query;

import cn.cjj.gulimall.product.dao.AttrGroupDao;
import cn.cjj.gulimall.product.entity.AttrGroupEntity;
import cn.cjj.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    @Autowired
    AttrGroupService attrGroupService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();

        String key = (String) params.get("key");
        if (!StringUtils.isNullOrEmpty(key)) {
            wrapper.and((obj) -> {
                obj.or().eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if (categoryId != 0) {
            wrapper.eq("catelog_id", categoryId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupEntities.stream().map((entity) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(entity, attrGroupWithAttrsVo);
            List<AttrEntity> attr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());

        return attrGroupWithAttrsVos;
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<ProductAttrValueEntity> productAttrValues = productAttrValueService.list(
                new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelations = attrAttrgroupRelationService.list();
        Map<Long, Long> attrId2AttrGroupId = attrAttrgroupRelations.stream().collect(
                Collectors.toMap(AttrAttrgroupRelationEntity::getAttrId, AttrAttrgroupRelationEntity::getAttrGroupId));

        List<AttrGroupEntity> attrGroups = attrGroupService.list();
        Map<Long, String> attrGroupId2attrGroupName = attrGroups.stream().collect(
                Collectors.toMap(AttrGroupEntity::getAttrGroupId, AttrGroupEntity::getAttrGroupName));

        Map<String, SkuItemVo.SpuItemAttrGroupVo> spuItemAttrGroups = new HashMap<>();
        productAttrValues.forEach(productAttrValue -> {
            Long attrId = productAttrValue.getAttrId();
            Long attrGroupId = attrId2AttrGroupId.get(attrId);
            String attrGroupName = attrGroupId2attrGroupName.get(attrGroupId);
            if (!spuItemAttrGroups.containsKey(attrGroupName)) {
                SkuItemVo.SpuItemAttrGroupVo spuItemAttrGroup = new SkuItemVo.SpuItemAttrGroupVo();
                spuItemAttrGroup.setGroupName(attrGroupName);
                spuItemAttrGroup.setAttrs(new ArrayList<>());
                spuItemAttrGroups.put(attrGroupName, spuItemAttrGroup);
            }
            SkuItemVo.SpuBaseAttrVo spuBaseAttr = new SkuItemVo.SpuBaseAttrVo();
            spuBaseAttr.setAttrName(productAttrValue.getAttrName());
            spuBaseAttr.setAttrValue(productAttrValue.getAttrValue());
            spuItemAttrGroups.get(attrGroupName).getAttrs().add(spuBaseAttr);
        });

        return new ArrayList<>(spuItemAttrGroups.values());
    }
}